package gui;

import file.SettingsDefinition;
import gui.bf3.BF3CheckBox;
import gui.bf3.BF3ComboBox;
import gui.bf3.BF3Setting;
import gui.bf3.BF3Slider;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import transparent.TransparentPanel;

public class AudioSettingsPanel extends TransparentPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5715269261943485305L;
	private static final int PANEL_WIDTH = 415;
	private static final int VOLUME_PANEL_HEIGHT = 165;
	private static final int AUDIO_PANEL_HEIGHT = 120;
	private static final int SPEAKER_PANEL_HEIGHT = 105;

	private BF3Slider dialogueVolume;
	private BF3Slider musicVolume;
	private BF3Slider volume;
	private BF3ComboBox soundSystem;
	private BF3ComboBox soundSystemSize;
	private BF3CheckBox soundSystemSubWoofer;
	private BF3CheckBox stereoMode;
	private BF3Slider audioQuality;
	private BF3ComboBox voLanguage;
	private BF3CheckBox carRadio;

	public AudioSettingsPanel(List<BF3Setting> settings) {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// Volume Slider Panel
		JPanel volumePanel = new TransparentPanel(new GridLayout(3, 2, 0, 5));
		volumePanel.setPreferredSize(new Dimension(PANEL_WIDTH, VOLUME_PANEL_HEIGHT));
		volumePanel.setMaximumSize(new Dimension(PANEL_WIDTH, VOLUME_PANEL_HEIGHT));
		volumePanel.setMinimumSize(new Dimension(PANEL_WIDTH, VOLUME_PANEL_HEIGHT));
		volumePanel.setBorder(BorderFactory.createTitledBorder("Volume"));

		volumePanel.add(new JLabel("Dialog Volume"));
		dialogueVolume = new BF3Slider(SettingsDefinition.DIALOGUEVOLUME);
		dialogueVolume.addChangeListener(new AudioChangeListener());
		settings.add(dialogueVolume);
		volumePanel.add(dialogueVolume);

		volumePanel.add(new JLabel("Music Volume"));
		musicVolume = new BF3Slider(SettingsDefinition.MUSICVOLUME);
		musicVolume.addChangeListener(new AudioChangeListener());
		settings.add(musicVolume);
		volumePanel.add(musicVolume);

		volumePanel.add(new JLabel("Game Volume"));
		volume = new BF3Slider(SettingsDefinition.VOLUME);
		volume.addChangeListener(new AudioChangeListener());
		settings.add(volume);
		volumePanel.add(volume);

		// Speaker Setup Panel
		JPanel speakerPanel = new TransparentPanel(new GridLayout(3, 2, 0, 5));
		speakerPanel.setPreferredSize(new Dimension(PANEL_WIDTH, SPEAKER_PANEL_HEIGHT));
		speakerPanel.setMaximumSize(new Dimension(PANEL_WIDTH, SPEAKER_PANEL_HEIGHT));
		speakerPanel.setMinimumSize(new Dimension(PANEL_WIDTH, SPEAKER_PANEL_HEIGHT));
		speakerPanel.setBorder(BorderFactory.createTitledBorder("Speaker"));

		speakerPanel.add(new JLabel("Sound System"));
		soundSystem = new BF3ComboBox(SettingsDefinition.YOURSOUNDSYSTEM, new String[] { "War Tapes", "TV", "Hi-Fi", "Home Cinema", "Headphones" });
		soundSystem.addActionListener(new AudioActionListener());
		settings.add(soundSystem);
		speakerPanel.add(soundSystem);

		speakerPanel.add(new JLabel("Sound System Size"));
		JPanel soundSystemSizePanel = new TransparentPanel();
		soundSystemSizePanel.setLayout(new BoxLayout(soundSystemSizePanel, BoxLayout.X_AXIS));
		soundSystemSize = new BF3ComboBox(SettingsDefinition.SOUNDSYSTEMSIZE, new String[] { "2", "3", "4", "5", "6", "7" });
		soundSystemSize.setActionCommand("soundSystemSize");
		soundSystemSize.addActionListener(new AudioActionListener());
		settings.add(soundSystemSize);
		soundSystemSizePanel.add(soundSystemSize);
		soundSystemSizePanel.add(new JLabel(" Subwoofer: "));
		soundSystemSubWoofer = new BF3CheckBox(SettingsDefinition.SOUNDSYSTEMSIZE);
		soundSystemSubWoofer.setActionCommand("soundSystemSubWoofer");
		soundSystemSubWoofer.addActionListener(new AudioActionListener());
		soundSystemSizePanel.add(soundSystemSubWoofer);
		settings.add(soundSystemSubWoofer);
		speakerPanel.add(soundSystemSizePanel);

		speakerPanel.add(new JLabel("Stereo Mode"));
		stereoMode = new BF3CheckBox(SettingsDefinition.STEREOMODE);
		stereoMode.addActionListener(new AudioActionListener());
		settings.add(stereoMode);
		speakerPanel.add(stereoMode);

		// General Audio Panel
		JPanel audioPanel = new TransparentPanel();
		audioPanel.setPreferredSize(new Dimension(PANEL_WIDTH, AUDIO_PANEL_HEIGHT));
		audioPanel.setMaximumSize(new Dimension(PANEL_WIDTH, AUDIO_PANEL_HEIGHT));
		audioPanel.setMinimumSize(new Dimension(PANEL_WIDTH, AUDIO_PANEL_HEIGHT));
		audioPanel.setBorder(BorderFactory.createTitledBorder("General"));
		audioPanel.setLayout(new BoxLayout(audioPanel, BoxLayout.Y_AXIS));

		JPanel audioSubPanel1 = new TransparentPanel(new GridLayout(1, 2, 0, 5));
		audioPanel.add(audioSubPanel1);
		audioSubPanel1.add(new JLabel("Audio Quality"));
		audioQuality = new BF3Slider(SettingsDefinition.AUDIOQUALITY);
		audioSubPanel1.add(audioQuality);

		JPanel audioSubPanel2 = new TransparentPanel(new GridLayout(2, 2, 0, 5));
		audioPanel.add(audioSubPanel2);
		audioSubPanel2.add(new JLabel("Voice Over Language"));
		voLanguage = new BF3ComboBox(SettingsDefinition.VOLANGUAGE, new String[] { "English" });
		voLanguage.addActionListener(new AudioActionListener());
		settings.add(voLanguage);
		audioSubPanel2.add(voLanguage);

		audioSubPanel2.add(new JLabel("Enable Car Radio"));
		carRadio = new BF3CheckBox(SettingsDefinition.CARRADIO);
		carRadio.addActionListener(new AudioActionListener());
		settings.add(carRadio);
		audioSubPanel2.add(carRadio);

		this.add(audioPanel);
		this.add(volumePanel);
		this.add(speakerPanel);
	}

	private class AudioActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("soundSystemSize")) {
				soundSystemSize.setValue((String) soundSystemSize.getSelectedItem());
			} else {
				if (e.getSource() instanceof BF3ComboBox) {
					BF3ComboBox cb = (BF3ComboBox) e.getSource();
					if (cb.getActionCommand().equals("soundSystemSize")) {
						cb.setValue(cb.getSelectedItem().toString());
					} else {
						cb.setValue(String.valueOf(cb.getSelectedIndex()));
					}
				} else if (e.getSource() instanceof BF3CheckBox) {
					BF3CheckBox cb = (BF3CheckBox) e.getSource();
					if (cb.isSelected()) {
						cb.setValue("1");
					} else {
						cb.setValue("0");
					}
				}

			}
		}
	}

	private class AudioChangeListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {
				if (e.getSource() instanceof BF3Slider) {
				BF3Slider sl = (BF3Slider) e.getSource();
				sl.setValue(String.valueOf(sl.getValue()));
			}

		}
	}
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Color ppColor = new Color(16, 16, 16, 100); //r,g,b,alpha
        g.setColor(ppColor);
        g.fillRect(+2,-2,PANEL_WIDTH-4,VOLUME_PANEL_HEIGHT+SPEAKER_PANEL_HEIGHT+AUDIO_PANEL_HEIGHT); //x,y,width,height
    }    
}
